package com.zyf.service.img;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class CuttingImgService {


    /**
     * 裁剪单张图片
     */
    public static String SingleImgCircleCutting(String imgPath) {
        try {
            System.out.println("开始裁剪单张图片=====");
            File[] imgs=new File(imgPath).listFiles();
            for(int i=0;i<imgs.length;i++){
                BufferedImage image = ImageIO.read(imgs[i]);
                for(int j=0;j<100;j++){
                    BufferedImage result = imageCutByRectangle(image,j*image.getWidth()/100,j,image.getWidth()/100,image.getHeight());
                    if(j==0){
                        ImageIO.write(result, "jpg", new File(imgPath+"\\"+imgs[i].getName()));
                    }else{
                        ImageIO.write(result, "jpg", new File(imgPath+"\\"+j+".jpg"));
                    }

                }
            }
            System.out.println("=====裁剪单张图片结束");
            return "操作成功";
        } catch (IOException e) {
            return e.toString();
        }
    }


    /**
     * 批量裁剪图片
     */
    public static String ImgCutBatch(String imgPath,int cutWidth,int cutHeight) {
        try {
            System.out.println("开始裁剪图片=====");
            File[] imgs=new File(imgPath).listFiles();
            for(int i=0;i<imgs.length;i++){
                String fileName=imgs[i].getName();
                String imgType="";
                if(fileName.endsWith(".jpg")){
                    imgType="jpg";
                }
                if(fileName.endsWith(".png")){
                    imgType="png";
                }
                BufferedImage image = ImageIO.read(imgs[i]);
                BufferedImage result = imageCutByRectangle(image,0,0,image.getWidth()-cutWidth,image.getHeight()-cutHeight);
                ImageIO.write(result, imgType, imgs[i].getAbsoluteFile());
            }
            System.out.println("=====裁剪图片结束");
            return "操作成功";
        } catch (IOException e) {
            return e.toString();
        }
    }

    /**
     * 矩形裁剪，设定起始位置，裁剪宽度，裁剪长度
     * 裁剪范围需小于等于图像范围
     *
     * @param image
     * @param xCoordinate
     * @param yCoordinate
     * @param xLength
     * @param yLength
     * @return
     */
    public static BufferedImage imageCutByRectangle(BufferedImage image, int xCoordinate, int yCoordinate, int xLength,
                                                    int yLength) {
        //判断x、y方向是否超过图像最大范围
        if ((xCoordinate + xLength) >= image.getWidth()) {
            xLength = image.getWidth() - xCoordinate;
        }
        if ((yCoordinate + yLength) >= image.getHeight()) {
            yLength = image.getHeight() - yCoordinate;
        }
        BufferedImage resultImage = new BufferedImage(xLength, yLength, image.getType());
        for (int x = 0; x < xLength; x++) {
            for (int y = 0; y < yLength; y++) {
                int rgb = image.getRGB(x + xCoordinate, y + yCoordinate);
                resultImage.setRGB(x, y, rgb);
            }
        }
        return resultImage;
    }

    /**
     * 圆形裁剪，定义圆心坐标，半径
     * 裁剪半径可以输入任意大于零的正整数
     *
     * @param image
     * @param xCoordinate
     * @param yCoordinate
     * @param radius
     * @return
     */
    public static BufferedImage imageCutByCircle(BufferedImage image, int xCoordinate, int yCoordinate, int radius) {
        //判断圆心左右半径是否超限
        if ((xCoordinate + radius) > image.getWidth() || radius > xCoordinate) {
            int a = image.getWidth() - 1 - xCoordinate;
            if (a > xCoordinate) {
                radius = xCoordinate;
            } else {
                radius = a;
            }
        }
        //判断圆心上下半径是否超限
        if ((yCoordinate + radius) > image.getHeight() || radius > yCoordinate) {
            int a = image.getHeight() - 1 - yCoordinate;
            if (a > yCoordinate) {
                radius = yCoordinate;
            } else {
                radius = a;
            }
        }
        int length = 2 * radius + 1;
        BufferedImage resultImage = new BufferedImage(length, length, image.getType());
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length; j++) {
                int x = i - radius;
                int y = j - radius;
                int distance = (int) Math.sqrt(x * x + y * y);
                if (distance <= radius) {
                    int rgb = image.getRGB(x + xCoordinate, y + yCoordinate);
                    resultImage.setRGB(i, j, rgb);
                }
            }
        }
        return resultImage;
    }



}
